﻿@model DatabasesModel
@{
    Layout = "Databases.Modal.cshtml";
    var s = Model.Instance;
    var views = s.GetViewInfo(Model.Database);
    var columns = s.GetColumnInfo(Model.Database);
    // TODO: Move this into core storage?
    var columnLookup = columns.Data
        .GroupBy(c => Tuple.Create(c.SchemaName, c.ViewName))
        .ToDictionary(g => g.Key, g => g.ToList());
}
@section top {
    <span class="pull-right small">@Poll.Now(views, columns)</span>
}
@if (views.Data == null || !views.Successful)
{
    <div class="alert alert-warning">
        <h5>There was an error fetching view info from @s.Name for @Model.Database</h5>
        <p class="error-stack">@views.ErrorMessage</p>
    </div>
}
else {
    <table class="table table-striped table-hover text-nowrap table-super-condensed table-responsive js-database-views">
        <thead>
            <tr>
                <th>View</th>
                <th>Created On</th>
                <th>Last Modified</th>
                <th>Replicated</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var v in views.Data.OrderBy(t => t.SchemaName).ThenBy(t => t.ViewName))
            {
                <tr data-obj="@(v.SchemaName).@(v.ViewName)" class="js-next-collapsible" title="@(v.SchemaName).@(v.ViewName) created @v.CreationDate">
                    <td>
                        <a href="#/db/@(Model.Database)/views/@(v.SchemaName).@(v.ViewName)">
                            <span class="text-muted">@(v.SchemaName).</span><span>@v.ViewName</span>
                        </a>
                    </td>
                    <td data-val="@v.CreationDate.ToEpochTime().ToString()">@v.CreationDate.ToRelativeTimeSpan()</td>
                    <td data-val="@v.LastModifiedDate.ToEpochTime().ToString()">@v.LastModifiedDate.ToRelativeTimeSpan()</td>
                    <td data-val="@v.IsReplicated.ToString()">@v.IsReplicated.ToYesNo()</td>
                </tr>
                <tr class="tablesorter-childRow hidden">
                    <td colspan="4">
                        <partial name="Databases.Modal.Columns" model="columnLookup[Tuple.Create(v.SchemaName, v.ViewName)]" />
                        <pre><code class="sql">@v.Definition.Trim()</code></pre>
                    </td>
                </tr>
                <tr class="tablesorter-childRow hidden"></tr>
            }
        </tbody>
        <tfoot>
            @if (!views.Data.Any())
            {
                <tr>
                    <td colspan="4">
                        <div class="no-content">There aren't any tables in this database.</div>
                    </td>
                </tr>
            }
        </tfoot>
    </table>
    <script>
        $(function () {
            $('.js-database-views').tablesorter({
                headers: {
                    1: { sorter: 'dataVal', sortInitialOrder: 'desc' },
                    2: { sorter: 'dataVal', sortInitialOrder: 'desc' }
                }
            });
            Status.highlight();
        });
    </script>
}